<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="送货地址" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.address`" :rules="formRules.address" class="mb-0px!">
            <el-input v-model="row.address" placeholder="请输入送货地址" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.remark`" :rules="formRules.remark" class="mb-0px!">
            <el-input v-model="row.remark" placeholder="请输入备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="联系人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.contacts`" :rules="formRules.contacts" class="mb-0px!">
            <el-input v-model="row.contacts" placeholder="请输入联系人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.phone`" :rules="formRules.phone" class="mb-0px!">
            <el-input v-model="row.phone" placeholder="请输入联系电话" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="区域id" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.regionId`" :rules="formRules.regionId" class="mb-0px!">
            <el-input v-model="row.regionId" placeholder="请输入区域id" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="区域名称" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.regionName`" :rules="formRules.regionName" class="mb-0px!">
            <el-select v-model="row.regionName" placeholder="请选择区域名称">
                <el-option
                  v-for="dict in getStrDictOptions(DICT_TYPE.DELIVERY_AREA)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="里程" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.distance`" :rules="formRules.distance" class="mb-0px!">
            <el-input v-model="row.distance" placeholder="请输入里程" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="里程单位" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.distanceUnit`" :rules="formRules.distanceUnit" class="mb-0px!">
            <el-input v-model="row.distanceUnit" placeholder="请输入里程单位" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="是否默认" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.defaultFlag`" :rules="formRules.defaultFlag" class="mb-0px!">
            <el-radio-group v-model="row.defaultFlag">
                <el-radio
                  v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                  :key="dict.value"
                  :label="dict.value"
                >
                  {{ dict.label }}
                </el-radio>
            </el-radio-group>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加ERP基础资料-客户送货地址</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseCustomerApi } from '@/api/erp/basecustomer'

const props = defineProps<{
  customerId: undefined // 客户id（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.customerId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await BaseCustomerApi.getBaseCustomerDeliveryAddressListByCustomerId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    customerId: undefined,
    address: undefined,
    remark: undefined,
    contacts: undefined,
    phone: undefined,
    regionId: undefined,
    regionName: undefined,
    distance: undefined,
    distanceUnit: undefined,
    defaultFlag: undefined
  }
  row.customerId = props.customerId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>